﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;
using Catering_Model;//Please add references
namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:Recipes
    /// </summary>
    public partial class D_Recipes
    {
        public D_Recipes()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Recipes");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_Recipes model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Recipes(");
            strSql.Append("CateringName,ClassID,GroupID,PID,StateDate,StopDate,Number,Back,AreaID,ClassIDs,temp1,temp2,IsBiaoZhun,EditPersonnelID,EditTime)");
            strSql.Append(" values (");
            strSql.Append("@CateringName,@ClassID,@GroupID,@PID,@StateDate,@StopDate,@Number,@Back,@AreaID,@ClassIDs,@temp1,@temp2,@IsBiaoZhun,@EditPersonnelID,@EditTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@CateringName", SqlDbType.VarChar,50),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@PID", SqlDbType.Int,4),
					new SqlParameter("@StateDate", SqlDbType.DateTime),
					new SqlParameter("@StopDate", SqlDbType.DateTime),
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@AreaID", SqlDbType.Int,4),
					new SqlParameter("@ClassIDs", SqlDbType.Int,4),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@IsBiaoZhun", SqlDbType.Bit,1),
					new SqlParameter("@EditPersonnelID", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.DateTime)};
            parameters[0].Value = model.CateringName;
            parameters[1].Value = model.ClassID;
            parameters[2].Value = model.GroupID;
            parameters[3].Value = model.PID;
            parameters[4].Value = model.StateDate;
            parameters[5].Value = model.StopDate;
            parameters[6].Value = model.Number;
            parameters[7].Value = model.Back;
            parameters[8].Value = model.AreaID;
            parameters[9].Value = model.ClassIDs;
            parameters[10].Value = model.temp1;
            parameters[11].Value = model.temp2;
            parameters[12].Value = model.IsBiaoZhun;
            parameters[13].Value = model.EditPersonnelID;
            parameters[14].Value = model.EditTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                model.id = Convert.ToInt32(obj);
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_Recipes model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Recipes set ");
            strSql.Append("CateringName=@CateringName,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("GroupID=@GroupID,");
            strSql.Append("PID=@PID,");
            strSql.Append("StateDate=@StateDate,");
            strSql.Append("StopDate=@StopDate,");
            strSql.Append("Number=@Number,");
            strSql.Append("Back=@Back,");
            strSql.Append("AreaID=@AreaID,");
            strSql.Append("ClassIDs=@ClassIDs,");
            strSql.Append("temp1=@temp1,");
            strSql.Append("temp2=@temp2,");
            strSql.Append("IsBiaoZhun=@IsBiaoZhun,");
            strSql.Append("EditPersonnelID=@EditPersonnelID,");
            strSql.Append("EditTime=@EditTime");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@CateringName", SqlDbType.VarChar,50),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@GroupID", SqlDbType.Int,4),
					new SqlParameter("@PID", SqlDbType.Int,4),
					new SqlParameter("@StateDate", SqlDbType.DateTime),
					new SqlParameter("@StopDate", SqlDbType.DateTime),
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@Back", SqlDbType.VarChar,200),
					new SqlParameter("@AreaID", SqlDbType.Int,4),
					new SqlParameter("@ClassIDs", SqlDbType.Int,4),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
					new SqlParameter("@IsBiaoZhun", SqlDbType.Bit,1),
					new SqlParameter("@EditPersonnelID", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.CateringName;
            parameters[1].Value = model.ClassID;
            parameters[2].Value = model.GroupID;
            parameters[3].Value = model.PID;
            parameters[4].Value = model.StateDate;
            parameters[5].Value = model.StopDate;
            parameters[6].Value = model.Number;
            parameters[7].Value = model.Back;
            parameters[8].Value = model.AreaID;
            parameters[9].Value = model.ClassIDs;
            parameters[10].Value = model.temp1;
            parameters[11].Value = model.temp2;
            parameters[12].Value = model.IsBiaoZhun;
            parameters[13].Value = model.EditPersonnelID;
            parameters[14].Value = model.EditTime;
            parameters[15].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Recipes ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Recipes ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_Recipes GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,CateringName,ClassID,GroupID,PID,StateDate,StopDate,Number,Back,AreaID,ClassIDs,temp1,temp2,IsBiaoZhun,EditPersonnelID,EditTime from Recipes ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_Recipes model = new E_Recipes();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CateringName"] != null)
                {
                    model.CateringName = ds.Tables[0].Rows[0]["CateringName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["GroupID"].ToString() != "")
                {
                    model.GroupID = int.Parse(ds.Tables[0].Rows[0]["GroupID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PID"].ToString() != "")
                {
                    model.PID = int.Parse(ds.Tables[0].Rows[0]["PID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StateDate"].ToString() != "")
                {
                    model.StateDate = DateTime.Parse(ds.Tables[0].Rows[0]["StateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StopDate"].ToString() != "")
                {
                    model.StopDate = DateTime.Parse(ds.Tables[0].Rows[0]["StopDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Number"].ToString() != "")
                {
                    model.Number = int.Parse(ds.Tables[0].Rows[0]["Number"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Back"] != null)
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["AreaID"].ToString() != "")
                {
                    model.AreaID = int.Parse(ds.Tables[0].Rows[0]["AreaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ClassIDs"].ToString() != "")
                {
                    model.ClassIDs = int.Parse(ds.Tables[0].Rows[0]["ClassIDs"].ToString());
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null)
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null)
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsBiaoZhun"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsBiaoZhun"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsBiaoZhun"].ToString().ToLower() == "true"))
                    {
                        model.IsBiaoZhun = true;
                    }
                    else
                    {
                        model.IsBiaoZhun = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["EditPersonnelID"].ToString() != "")
                {
                    model.EditPersonnelID = int.Parse(ds.Tables[0].Rows[0]["EditPersonnelID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,CateringName,ClassID,GroupID,PID,StateDate,StopDate,Number,Back,AreaID,ClassIDs,temp1,temp2,IsBiaoZhun,EditPersonnelID,EditTime ");
            strSql.Append(" FROM Recipes ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from (select A.*,B.AID from Recipes as A left join ClassInfo as B on A.ClassIDs=B.id) as T ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取数据列表 支持排序查询
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="Order"></param>
        /// <returns></returns>
        public DataSet GetList(string strWhere, string Order)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from (
                                select A.*,B.AID,C.Name as EditPersonnelName from Recipes as A 
                                left join ClassInfo as B on A.ClassIDs=B.id
                                left join Personnel as C on A.EditPersonnelID=C.id
                            ) as T");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (Order.Trim() != "")
            {
                strSql.Append(" order by " + Order);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateIsBiaoZhun(int ID, int Flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Recipes set ");
            strSql.Append("IsBiaoZhun=@IsBiaoZhun");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@IsBiaoZhun", SqlDbType.Bit,2),					
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = Flag;
            parameters[1].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdateCateringName(int ID, string CateringName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Recipes set ");
            strSql.Append("CateringName=@CateringName");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@CateringName", SqlDbType.VarChar,50),					
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = CateringName;
            parameters[1].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取食谱最大的时间
        /// </summary>
        /// <param name="classid"></param>
        /// <returns></returns>
        public DateTime GetRecipesMaxDateTime(int classid)
        {
            string sql = "select Top 1 StopDate from Recipes where ClassID=" + classid + " order by StopDate DESC ";
            Object result = DbHelperSQL.GetSingle(sql);
            DateTime dtime = DateTime.Now;
            if (result != null) DateTime.TryParse(result.ToString(), out dtime);
            return dtime;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "Recipes";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("GetPageData", parameters, "Recipes");
        }

        /// <summary>
        /// 获取符合要求的食谱
        /// </summary>
        public DataSet GetRecipesByInfo(string ClassID, string starttime, string endtime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from Recipes where id in (");
            sb.Append("select a.MID from RecipeInformation as a ");
            sb.Append("join Recipes as b on a.MID=b.id ");
            sb.Append("where b.ClassID=" + ClassID + " and a.Date>=convert(datetime,'" + starttime + "') and a.Date<=convert(datetime,'" + endtime + "')");
            sb.Append("group by a.MID )");
            return DbHelperSQL.Query(sb.ToString());
        }

        /// <summary>
        /// 通过条件获取原料总和以及折损率
        /// </summary>
        public DataSet GetRecipesYLCount(string RecipesIDlist, string starttime, string endtime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select b.RawID,sum(convert(decimal(18,2),b.Number)*d.Number) as Content,c.temp1 as zsl ");
            sb.Append("from RecipeInformation as a right join RecipeInformationInfo as b on a.id=b.RecipeInformationID  ");
            sb.Append("join RawMaterial as c on b.RawID=c.id ");
            sb.Append("join dbo.Recipes as d on a.MID=d.id ");
            sb.Append("where a.MID in (" + RecipesIDlist + ") and a.Date>=convert(datetime,'" + starttime + "') and a.Date<=convert(datetime,'" + endtime + "')");
            sb.Append("group by b.RawID,c.temp1");
            return DbHelperSQL.Query(sb.ToString());
        }

        /// <summary>
        /// 通过条件获取原料总和以及折损率
        /// </summary>
        public DataSet GetRecipesYLCount(string whereClause)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select b.RawID,sum(convert(decimal(18,2),b.Number)*d.Number) as Content,c.temp1 as zsl ");
            sb.Append("from RecipeInformation as a right join RecipeInformationInfo as b on a.id=b.RecipeInformationID  ");
            sb.Append("join RawMaterial as c on b.RawID=c.id ");
            sb.Append("join dbo.Recipes as d on a.MID=d.id ");
            sb.Append("where 1=1 " + whereClause + " ");
            sb.Append("group by b.RawID,c.temp1");
            return DbHelperSQL.Query(sb.ToString());
        }

        /// <summary>
        /// 通过条件获取每天每人消耗量
        /// </summary>
        public DataSet GetRecipesYLday(string RecipesIDlist, string starttime, string endtime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select c.RAWName,b.RawID,sum(convert(decimal(18,2),b.Number)) as Content ");
            sb.Append("from RecipeInformation as a right join RecipeInformationInfo as b on a.id=b.RecipeInformationID ");
            sb.Append("join RawMaterial as c on b.RawID=c.id ");
            sb.Append("join dbo.Recipes as d on a.MID=d.id ");
            sb.Append("where a.MID in (" + RecipesIDlist + ") and a.Date>=convert(datetime,'" + starttime + "') and a.Date<=convert(datetime,'" + endtime + "')");
            sb.Append("group by b.RawID,c.RAWName");
            return DbHelperSQL.Query(sb.ToString());
        }

        /// <summary>
        /// 通过条件获取每天每人消耗量
        /// </summary>
        public DataSet GetRecipesYLday(string whereClause)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select c.RAWName,b.RawID,sum(convert(decimal(18,2),b.Number)) as Content ");
            sb.Append("from RecipeInformation as a right join RecipeInformationInfo as b on a.id=b.RecipeInformationID ");
            sb.Append("join RawMaterial as c on b.RawID=c.id ");
            sb.Append("join dbo.Recipes as d on a.MID=d.id ");
            sb.Append("where 1=1 " + whereClause + " ");
            sb.Append("group by b.RawID,c.RAWName");
            return DbHelperSQL.Query(sb.ToString());
        }

        /// <summary>
        /// 获取非个人食谱集合
        /// </summary>
        /// <param name="IDS"></param>
        /// <param name="ProID"></param>
        /// <returns></returns>
        public DataSet GetNotMyList(string IDS, int EditPersonnelID)
        {
            StringBuilder SQL = new StringBuilder();
            SQL.Append("select * from dbo.Recipes where id in (" + IDS + ") and (EditPersonnelID!=" + EditPersonnelID + " or EditPersonnelID is null)");
            return DbHelperSQL.Query(SQL.ToString());
        }
    }
}

